package pageRank;

import com.sun.org.apache.xerces.internal.xs.ShortList;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

/**
 * TextRank算法 提取关键词
 * @author yuyufeng
 * @date 2017/11/3
 */
public class Demo1 {
    public static void main(String[] args) {
        Map<String, Float> keyTimes = new HashMap<>();
        List<String> keyWords = new ArrayList<>();
        int k = 5;  //窗口大小/2
        float d = 0.85f;
        /**
         * 标点符号、常用词、以及“名词、动词、形容词、副词之外的词”
         */
        Set<String> stopWordSet = new HashSet<String>();
        stopWordSet.add("是");
        stopWordSet.add("的");
        stopWordSet.add("将");
        stopWordSet.add("但");
        stopWordSet.add("和");
        stopWordSet.add("为");
        stopWordSet.add("让");
        String field = "程序员(英文Programmer)是从事程序开发、维护的专业人员。一般将程序员分为程序设计人员和程序编码人员，但两者的界限并不非常清楚，特别是在中国。软件从业人员分为初级程序员、高级程序员、系统分析员和项目经理四大类。";


        Analyzer analyzer = new IKAnalyzer(true);
        TokenStream ts = null;
        //分词
        try {
            ts = analyzer.tokenStream("myfield", new StringReader(field));
            OffsetAttribute offset = (OffsetAttribute)ts.addAttribute(OffsetAttribute.class);
            CharTermAttribute term = (CharTermAttribute)ts.addAttribute(CharTermAttribute.class);
            TypeAttribute type = (TypeAttribute)ts.addAttribute(TypeAttribute.class);
            ts.reset();

            while(ts.incrementToken()) {
                if(!stopWordSet.contains(term.toString())){
                    keyWords.add(term.toString());
                }
            }
            ts.end();
        } catch (IOException var14) {
            var14.printStackTrace();
        } finally {
            if (ts != null) {
                try {
                    ts.close();
                } catch (IOException var13) {
                    var13.printStackTrace();
                }
            }

        }

        //迭代传播各节点的权重
        for (int i = 0; i < keyWords.size(); i++) {
            for(int j = i - k;j < i + k;j ++){
                if(j < 0 || j >= keyWords.size()){
                    continue;
                }

                String key = keyWords.get(j);
                if(key != null){
                    if(keyTimes.get(key)==null){
                        keyTimes.put(key,1-d);
                    }else{
                        keyTimes.put(key,keyTimes.get(key)+(1-d));
                    }
                }
            }
        }

        //排序
        List<KeyWords> keyWordsList = new LinkedList<>();
        for (String key : keyTimes.keySet()) {
            KeyWords kw = new KeyWords();
            kw.key = key;
            kw.significance = keyTimes.get(key);
            keyWordsList.add(kw);
        }

        keyWordsList.sort(new Comparator<KeyWords>() {
            @Override
            public int compare(KeyWords o1, KeyWords o2) {
                if(o2.significance - o1.significance > 0){
                    return  1;
                }else if(o2.significance - o1.significance < 0 ){
                    return -1;
              }
                return 0;
            }
        });

        int len = 100;
        //前20个关键词
        for (int i = 0; i < (keyWordsList.size() > len ? len:keyWordsList.size()); i++) {
            System.out.println(keyWordsList.get(i));
        }
    }


}

class KeyWords{
    String key;
    float significance;

    @Override
    public String toString() {
        return "关键词=" + key  +
                ", 重要程度=" + significance;
    }
}